package com.none.web.controller.fileupload;

import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.none.core.common.utils.JSONUtil;
import com.none.core.controller.BaseControllerSupport;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;

/**
 * 文件上传Controller
 * 
 * @author porterzhang
 * 
 */
@Controller
@RequestMapping("fileupload")
public class FileUploadController extends BaseControllerSupport {
	private static String PATH_FOLDER = "/";
	private static String TEMP_FOLDER = "/";
	
	

	/**
	 * 文件上传
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("upLoadFile")
	public Object upLoadFile(MultipartFile myfile) throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		Result result;
		if (myfile==null) {
			result = new Result(SysConstant.STATE_ERROR, "fileupload.uploaderror", data);
			return JSONUtil.toJSON(result);
		} else {
			// 如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
			String rootPath = getSession().getServletContext().getRealPath("");
			String realPath = getSession().getServletContext().getRealPath("/upload");
			// 得到当前时间
			Calendar cal = Calendar.getInstance();// 使用日历类
			int year = cal.get(Calendar.YEAR);// 得到年
			int month = cal.get(Calendar.MONTH) + 1;// 得到月，因为从0开始的，所以要加1
			int day = cal.get(Calendar.DAY_OF_MONTH);// 得到天
			realPath = realPath + "/" + year + "" + month + "/" + day;
			// 循环创建文件夹
			File file = new File(realPath);
			file.mkdirs();
			String myfilename = myfile.getOriginalFilename();

			String newFileName = new Date().getTime() + "." + myfilename.substring(myfilename.indexOf(".") + 1);

			// 这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
			FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, newFileName));

			String url = getRequest().getRequestURL().toString();
			String uri = getRequest().getRequestURI().toString();
			String domain = url.replace(uri, "");
			String path = getRequest().getContextPath();
			data.put("filepath", domain + "" + path + "" + realPath.replace(rootPath, "") + "/" + newFileName);
			result = new Result(SysConstant.STATE_SUCCESS, "fileupload.uploadsuccess", data);
		}
		return JSONUtil.toJSON(result);
	}


}
